package com.yunkaweilai.android.activity.login;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.shop.CompanyTypeActivity1;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.login.IndustryModel;
import com.yunkaweilai.android.model.shop.CompanyTypeModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.dialog.shop.ShopCompanyAddDialog;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 行业列表
 * Created by Administrator on 2017/7/24.
 */

public class IndustryActivity extends BaseActivity {

    @BindView(R.id.id_listview)
    ListView idListview;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    public static final String INDUSTRY_NAME = "INDUSTRY_NAME";
    public static final String INDUSTRY_CODE = "INDUSTRY_CODE";
    private ArrayList<IndustryModel.DataBean.IndustryListBean> list = new ArrayList<>();
    private CommonAdapter<IndustryModel.DataBean.IndustryListBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_type_company1);
        ButterKnife.bind(this);
        new TitleBuilder(this).setLeftImage(R.mipmap.ic_go_back)
                .setTitleText("选择行业").setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        initAdapter();
        initClick();
        initHttp();
    }

    private void initAdapter() {
        adapter = new CommonAdapter<IndustryModel.DataBean.IndustryListBean>(this,R.layout.item_list_company,list) {
            @Override
            protected void convert(ViewHolder viewHolder, IndustryModel.DataBean.IndustryListBean item, int position) {
                viewHolder.setText(R.id.id_tv_company,item.getIndustry_name());
            }
        };
        idListview.setAdapter(adapter);
    }

    private void initClick() {
        idListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.putExtra(INDUSTRY_NAME, list.get(position).getIndustry_name());
                intent.putExtra(INDUSTRY_CODE,list.get(position).getIndustry_code());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.INDUSTRY_LIST)
                .log(TAG).execute(new PostProcess.StringCallBack() {
            @Override
            public void onResponse(String json) {
                Gson gson = new Gson();
                boolean checkCode = Utils.checkCode(mAc, json);
                if (checkCode) {
                    IndustryModel industryModel = gson.fromJson(json, IndustryModel.class);
                    list.clear();
                    list.addAll(industryModel.getData().getIndustryList());
                    adapter.notifyDataSetChanged();
                    if (idMultipleStatusView != null){
                        idMultipleStatusView.showContent();
                    }
                } else {
                    if (idMultipleStatusView != null){
                        idMultipleStatusView.showError();
                    }
                }
            }

            @Override
            public void onError(Exception e) {
                if (idMultipleStatusView != null){
                    idMultipleStatusView.showError();
                }
            }
        });
    }

}
